/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.upao.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Rey
 */
public class FeedbackServlet extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        resp.setContentType("text/html;charset=UTF-8");
        PrintWriter out = resp.getWriter();
        try {
            String nombres=req.getParameter("nombres");
            String nombre_curso=req.getParameter("nombre_curso");
            String name_r = req.getParameter("name_r");
            String email_r = req.getParameter("email_r");
            String name_d = req.getParameter("name_d");
            String email_d = req.getParameter("email_d");
            String description = req.getParameter("description");
            String subject = req.getParameter("subject");
            Properties props = new Properties();
            Session session = Session.getDefaultInstance(props, null);

            String msgBody = name_r + "\n" + description + "\n" + email_r;
            String alerta = "Este mensaje ha sido enviado desde una aplicación PÚBLICA de GoogleAppEngine y NO EXPRESA el sentimiento de su autor.";


            try {
                Message msg = new MimeMessage(session);
                msg.setFrom(new InternetAddress("reyann11@gmail.com", name_r));
                msg.addRecipient(Message.RecipientType.TO, new InternetAddress(email_d, name_d));
                msg.setSubject(subject);
                msg.setText(description + "\n" + "\n" + "\n" + "\n" + "--------" + "\n" + alerta);
                Transport.send(msg);

            } catch (Exception e) {
                resp.setContentType("text/plain");
                resp.getWriter().println("Algo salió mal, intenta de nuevo.");
                throw new RuntimeException(e);
            }

            //RequestDispatcher rd = getServletContext().getRequestDispatcher("/mailConfirmed.jsp");
            
            String path="/curso?action=cambioPagina&nombres="+nombres+"&path=/JSP/V_MenuConfiguracion.jsp&nombre_curso="+nombre_curso;
            RequestDispatcher rd = getServletContext().getRequestDispatcher("path");
            rd.forward(req, resp);
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
